package com.example.mall.dao;

import com.example.mall.model.Contain;
import com.mysql.cj.result.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ContainDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Contain contain) {
        String sql = "insert into contain (oID, cID, num) values (?, ?, ?)";
        jdbcTemplate.update(sql, contain.getoID(), contain.getcID(), contain.getNum());
    }

    public void deleteByOid(Integer oID) {
        String sql = "delete from contain where oID = ?";
        jdbcTemplate.update(sql, oID);
    }

    public List<Contain> getByOid(Integer oID) {
        String sql = "select * from contain where oID = ? lock in share mode";
        RowMapper<Contain> rowMapper = new BeanPropertyRowMapper<>(Contain.class);
        return jdbcTemplate.query(sql, rowMapper, oID);
    }

    public List<Contain> getAll() {
        String sql = "select * from contain lock in share mode";
        RowMapper<Contain> rowMapper = new BeanPropertyRowMapper<>(Contain.class);
        return jdbcTemplate.query(sql, rowMapper);
    }

}
